#include "stdafx.h"
#include "Fault.h"

#include "../../../Framework/Toolkit.h"

using namespace Framework;

namespace MyGame {

Fault::Fault(void) {
    this->lines = 16;
    this->minDisplacement = 0.1f;
    this->maxDisplacement = 0.2f;
}

Fault::~Fault(void) {
}

int Fault::GetLines() const {
    return this->lines;
}

void Fault::SetLines(int val) {
    this->lines = val;
}

FLOAT Fault::GetMaxDisplacement() const {
    return this->maxDisplacement;
}

void Fault::SetMaxDisplacement(FLOAT val) {
    this->maxDisplacement = val;
}

FLOAT Fault::GetMinDisplacement() const {
    return this->minDisplacement;
}

void Fault::SetMinDisplacement(FLOAT val) {
    this->minDisplacement = val;
}

void Fault::Generate(HeightMap* heightMap) {
    int length = heightMap->GetLength();
    int width = heightMap->GetWidth();
    FLOAT** map = heightMap->GetMap();
    for (int i = 0; i < lines; ++i) {
        int x1 = rand() % width;
        int y1 = rand() % length;
        int x2 = rand() % width;
        int y2 = rand() % length;
        FLOAT height;
        if (minDisplacement = maxDisplacement) {
            height = minDisplacement;
        } else {
            height = (FLOAT)Toolkit::RandomFloat(minDisplacement, maxDisplacement);
        }

        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < length; ++y) {
                if ((x2-x1)*(y-y1)-(y2-y1)*(x-x1)>0) {
                    map[x][y] += height;
                } else {
                    map[x][y] -= height;
                }
            }
        }
    }
}

}
